package main.字形排列;

public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String result = solution.convert("A", 1);
//PAHNAPLSIIGYIR
        System.out.println(result);
    }

    public String convert(String s, int numRows) {

        if(numRows == 1){
            return s;
        }

        int itemSize = numRows * 2 - 2;

        int size = s.length();

        int count = size / itemSize;
        int yu = size % itemSize;

        int row = numRows;
        int cloumns = (1 + numRows - 2) * count;

        if(yu >= numRows){
            cloumns++;
            yu -= numRows;

            cloumns += yu;
        }
        else if(yu > 0){
            cloumns++;
        }
        char[][] array = new char[numRows][cloumns];

        int index = 0;

        int cycle = numRows - 1;
        out:
        for (int i = 0; i < cloumns; i++) {
            if(cycle == numRows - 1){
                for (int j = 0; j < numRows; j++) {
                    array[j][i] = s.charAt(index++);
                    if(index == s.length()){
                        break out;
                    }
                }
            }
            else{

                array[cycle][i] = s.charAt(index++);
                if(index == s.length()){
                    break out;
                }

            }

            cycle --;
            if(cycle == 0){
                cycle = numRows - 1;
            }

        }

        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < cloumns; j++) {

                if(array[i][j] != '\u0000'){
                    stringBuilder.append(array[i][j]);
                }
            }
        }

        return stringBuilder.toString();
    }
}
